package cn.zzu.faceorgans;

import androidx.appcompat.app.AppCompatActivity;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class CanvaActivity extends AppCompatActivity {

    ImageView iv;
    Button btn1,btn2,btn3,btn4;
    Resources r;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_canva);
        iv=(ImageView)this.findViewById(R.id.ImageView01);
        btn1=(Button)this.findViewById(R.id.Button01);
        btn2=(Button)this.findViewById(R.id.Button02);
        btn3=(Button)this.findViewById(R.id.Button03);

        btn1.setOnClickListener(new ClickEvent());
        btn2.setOnClickListener(new ClickEvent());
        btn3.setOnClickListener(new ClickEvent());

        r = this.getResources();


    }
    class ClickEvent implements View.OnClickListener {

        public void onClick(View v) {
            if(v==btn1)//显示资源图片
            {//功能等效
                //iv.setBackgroundResource(R.drawable.icon);//打开资源图片
                Bitmap bmp= BitmapFactory.decodeResource(r, R.drawable.ic_launcher_background);//打开资源图片
                iv.setImageBitmap(bmp);
            }
            else if(v==btn2)//显示并绘画资源图片
            {
                Bitmap bmp=BitmapFactory.decodeResource(r, R.drawable.ic_launcher_background);//只读,不能直接在bmp上画
                Bitmap newb = Bitmap.createBitmap( 300, 300, Bitmap.Config.ARGB_8888 );

                Canvas canvasTemp = new Canvas( newb );
                canvasTemp.drawColor(Color.TRANSPARENT);

                Paint p = new Paint();
                String familyName ="宋体";
                Typeface font = Typeface.create(familyName,Typeface.BOLD);
                p.setColor(Color.RED);
                p.setTypeface(font);
                p.setTextSize(22);
                canvasTemp.drawText("写字。。。",50,50,p);
//                canvasTemp.drawBitmap(bmp, 50, 50, p);//画图
                iv.setImageBitmap(newb);
            }
            else if(v==btn3)//直接在Button上绘图
            {
                Bitmap newb = Bitmap.createBitmap( btn3.getWidth(), btn3.getHeight(), Bitmap.Config.ARGB_8888 );
                Canvas canvasTemp = new Canvas( newb );
                canvasTemp.drawColor(Color.WHITE);
                Paint p = new Paint();
                String familyName = "宋体";
                Typeface font = Typeface.create(familyName, Typeface.BOLD);
                p.setColor(Color.RED);
                p.setTypeface(font);
                p.setTextSize(20);
                canvasTemp.drawText("写字。。。", 30, 30, p);
                Drawable drawable = new BitmapDrawable(newb);
                btn3.setBackgroundDrawable(drawable);
            }
        }

    }


}